function replyComment(commentId) {
	if (parseInt(commentId) < 1) {
		return layer.msg('回复对像错误！');
	}
	// 评论必须先登陆
	isLogin(function () {
		layer.open({
			title: '回复评论',
			type: 2,
			area: isMobileBrowser() ? ['100%', '450px'] : ['800px', '450px'],
			content: '/comment/reply.html?commentId=' + commentId
		});
	});
}
// 添加评论
function addComment(otherId, type) {
	if (parseInt(otherId) < 1) {
		return layer.msg('评论对像错误！');
	}
	if (parseInt(type) < 1) {
		return layer.msg('评论参数错误！');
	}
	// 评论必须先登陆
	isLogin(function () {
		layer.open({
			title: '发表评论',
			type: 2,
			area: isMobileBrowser() ? ['100%', '400px'] : ['800px', '360px'],
			content: '/comment/add.html?otherId=' + otherId + '&type=' + type
		});
	});
}
function praiseComment(commentId) {
	$.ajax({
		url: "/ajax/comment/praise.json",
		data: {"id": commentId},
		type: "post",
		dataType: "json",
		async: true,
		success: function (result) {
			if (result.code === 0) {
				var obj = $("#praise_count_" + commentId);
				obj.html(parseInt(obj.html()) + 1);
				layer.msg('恭喜您，点赞成功！');
			} else {
				layer.msg(result.msg);
			}
		}
	})
}

function commentPost(data, callback) {
	// 点击时清空错误提示
	data.parentId = data.parentId || 0;
	data.content = data.content || '';
	data.type = data.type || 0;
	data.otherId = data.otherId || 0;
	// 评论内容不能为空
	if (data.content.length < 5) {
		return layer.msg("评论内容过于简单，请最少输入不少于5个字符的评论内容！", {icon: 2});
	}
	data.content = encodeURI(data.content);
	$.ajax({
		url: '/comment/add.json',
		type: 'post',
		data: data,
		dataType: 'json',
		success: callback
	});
}

function commentListInit(params) {
	params.pageSize = params.pageSize || 8;
	params.page = params.page || 1;
	$.ajax({
		type: "POST",
		dataType: "json",
		url: "/ajax/comment/count.json",
		data: params,
		cache: true,
		success: function (json) {
			//console.info("json:"+json);
			if (json.data > 0) {
				params.totalCount = json.data;
				layui.laypage.render({
					elem: params.pageViewId.replace('#', '')
					, limit: params.pageSize
					, count: params.totalCount //数据总数，从服务端得到
					, jump: function (obj, first) {
						params.page = obj.curr;
						//obj包含了当前分页的所有参数，比如：
						//console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
						//console.log(obj.limit); //得到每页显示的条数
						//首次不执行
						//if (!first) {
						commentListBind(params);
						//}
					}
				});
			}
		}
	});
}
function commentListBind(params) {
	$.ajax({
		type: "POST",
		dataType: "json",
		url: "/ajax/comment/list.json",
		data: params,
		cache: true,
		success: function (json) {
			if (json.code === 0) {
				layui.laytpl($(params.templateId).html()).render(json.data, function (string) {
					$(params.viewId).html(string)
				});
			}
		}
	});
}
